package com.cqeec.waimai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqeec.waimai.common.R;
import com.cqeec.waimai.dto.DishDto;
import com.cqeec.waimai.entity.*;
import com.cqeec.waimai.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j

public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    SetmealService setmealService;

    @PostMapping
    public R<String> addDishWithFlavor(@RequestBody DishDto dishDto) {
        dishService.saveWithFlavors(dishDto);
        return R.success("新增菜品成功");
    }

    //    菜品管理页面的分页查询
    @GetMapping("/page")
    public R<Page> getPage(int page, int pageSize, String name) {
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Dish::getName, name);
        dishService.page(pageInfo, wrapper);
//        为了DishDto categoryName  有值
        Page<DishDto> dtoPage = new Page<>(page, pageSize);
//        对象拷贝
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        List<DishDto> list = pageInfo.getRecords().stream().map(item -> {
//            每一个item相当于每一条菜品数据
            DishDto dishDto = new DishDto();
//            将数据item的值 ，拷贝给dishDto对象
            BeanUtils.copyProperties(item, dishDto);
//            获取数据库中的category_id这个字段的值，然后在通过这个id查询category这张表的数据
            Category category = categoryService.getById(item.getCategoryId());
//            如果前面查询到了数据
            if (category != null) {
//                给dishDto 里的category设置值，从查询结果中获取出来的
                dishDto.setCategoryName(category.getName());
            }
            return dishDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
//        将处理后的list设置records
        return R.success(dtoPage);
    }

    //   修改
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable("id") Long id) {
        DishDto dishDto = dishService.getByIdFlavor(id);
        return R.success(dishDto);

    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }
//    套餐管理页面，选择菜品
    @GetMapping("/list")
    public R<List<Dish>> getDish(Dish dish){
//        根据分类的id查询相关的菜品，返回给前端页面
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
//        查询正在售卖的菜品
        wrapper.eq(Dish::getStatus,1);
        wrapper.orderByAsc(Dish::getSort).orderByAsc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(wrapper);
        return R.success(list);
    }
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable("status")int status,@RequestParam List<Long>ids){
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.in(ids !=null,Dish::getId,ids);
        List<Dish> list =dishService.list(queryWrapper);

        for(Dish dish:list){
            if (dish != null){
                dish.setStatus(status);
                dishService.updateById(dish);
            }
        }
        return R.success("售卖状态修改成功");
    }
    @DeleteMapping
    public R<String> delete(@RequestParam("ids")List<Long>ids) {
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getDishId,ids);
        List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);
        if (setmealDishList != null) {
            dishService.deleteById(ids);
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(DishFlavor::getDishId, ids);
            dishFlavorService.remove(queryWrapper);
            return R.success("菜品删除成功");
        }
        ArrayList<Long> Setmeal_idList = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishList) {
            Long setmealId = setmealDish.getSetmealId();
            Setmeal_idList.add(setmealId);
        }
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId,setmealDishList);
        List<Setmeal> setmealList = setmealService.list(setmealLambdaQueryWrapper);
        for(Setmeal setmeal : setmealList){
            Integer status = setmeal.getStatus();
            if(status == 1){
                return R.error("删除菜品中不能有关联的在售套餐不能删除");
            }
        }
        dishService.deleteById(ids);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(queryWrapper);
        return R.success("菜品删除成功");
    }

}
